 <?php

/**
 * 预购商品
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

assign_template();
$position = assign_ur_here();
$smarty->assign('page_title', $position['title']);    // 页面标题


/*判断页面是否在允许时间内*/
if ($_CFG['is_yugou'] == 0) {
 header('location:index.php');
}





// 获取预购商品
$pre_order_goods = $GLOBALS['mem']->get(VERSION.'pre_order_goods');
if (empty($pre_order_goods)) {
    $pre_order_goods = get_pre_order_goods();
    $GLOBALS['mem']->set(VERSION.'pre_order_goods', $pre_order_goods, 180);
}

/*重组数据*/
foreach ($pre_order_goods as $key => $value) {
    $goods_list[$value['pre_order_part']][] =$value;
}
/*键值排序*/
ksort($goods_list);
$smarty->assign('goods_list',$goods_list);
// $smarty->assign('pre_order_goods',$pre_order_goods);
$smarty->assign('script_name', 'pre_order');
$smarty->display('pre_order.dwt');


/**
 * 获得预购商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function get_pre_order_goods(){
    $nowTime = time(); //当前时间戳
    $sql = 'SELECT po.pre_order_num, po.limit_num, po.buy_num, po.buy_number, po.pre_order_part, po.start_time, po.end_time, po.pre_order_price, po.goods_sku, g.goods_id, g.goods_name, g.goods_sku, g.shop_price, g.market_price, g.goods_thumb, g.goods_img, gs.goods_number, gs.goods_sku, ga.goods_attr_id ' . 
        ' FROM ' . $GLOBALS['ecs']->table('pre_order') . ' AS po ' . 
        " LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g " . 
        " ON g.goods_sku = po.goods_sku " . 
        " LEFT JOIN " . $GLOBALS['ecs']->table('goods_stock') . " AS gs " . 
        " ON gs.goods_sku = po.goods_sku " . 
        " LEFT JOIN " . $GLOBALS['ecs']->table('goods_attr') . "AS ga " .
        " ON ga.goods_id = g.goods_id " . 
        " WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND gs.area_id = 3 AND po.end_time >= '$nowTime' ORDER BY po.sort ASC, po.pre_order_id ASC";
    $res = $GLOBALS['db']->query($sql);
    
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = "goods_pre.php?id=$row[goods_id]";
        $arr[$row['goods_id']]['pre_order_part']   = $row['pre_order_part']; //第几块区域
        $arr[$row['goods_id']]['pre_order_price']  = $row['pre_order_price'];//预购价格
        $arr[$row['goods_id']]['can_buy_num']      = min(($row['pre_order_num']-$row['buy_num']),$row['goods_number']); //可购买数量
        $arr[$row['goods_id']]['surplus_num']      = intval((min(($row['pre_order_num']-$row['buy_num']),$row['goods_number'])/$row['pre_order_num'])*$row['buy_number']);//剩余件数
        $arr[$row['goods_id']]['is_attr']          = empty($row['goods_attr_id'])?0:1;
        if($row['start_time'] <= time() && time() <= $row['end_time']){
            $arr[$row['goods_id']]['is_buy']   = 1;
        }else{
            $arr[$row['goods_id']]['is_buy']   = 0;
        }
    }
    return $arr;
}

?>
